battmon2 2.2.1
Loading...
Searching...
No Matches
battmon2


BATT-MON 2 Click

BATT-MON 2 Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.


Click Library

  • Author : Stefan Filipovic
  • Date : Jan 2022.
  • Type : I2C type

Software Support

Example Description

This example demonstrates the use of BATT-MON 2 Click board by monitoring the measurements of battery voltage, current, capacity, percentage, time-to-empty or time-to-full, as well as the chip internal temperature.

Example Libraries

  • MikroSDK.Board
  • MikroSDK.Log
  • Click.BATTMON2

Example Key Functions

Application Init

Initializes the driver and checks the communication by reading and verifying the device ID.

void application_init ( void )
{
log_cfg_t log_cfg;
battmon2_cfg_t battmon2_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
battmon2_cfg_setup( &battmon2_cfg );
if ( I2C_MASTER_ERROR == battmon2_init( &battmon2, &battmon2_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
{
log_error( &logger, " Check communication." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}
@ BATTMON2_ERROR
Definition battmon2.h:266
#define BATTMON2_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition battmon2.h:218
err_t battmon2_check_communication(battmon2_t *ctx)
BATT-MON 2 check communication function.
void application_init(void)
Definition main.c:34
#define MIKROBUS_POSITION_BATTMON2
Definition main.c:28

Application Task

Reads and displays on the USB UART the measurements of battery voltage, current, capacity, percentage, time-to-empty or time-to-full, as well as the chip internal temperature approximately once per second.

void application_task ( void )
{
float voltage, capacity, percentage, current, die_temp;
if ( BATTMON2_OK == battmon2_get_battery_voltage ( &battmon2, &voltage ) )
{
log_printf ( &logger, " Voltage: %.1f mV \r\n", voltage );
}
if ( BATTMON2_OK == battmon2_get_battery_current ( &battmon2, &current ) )
{
log_printf ( &logger, " Current: %.1f mA \r\n", current );
}
if ( BATTMON2_OK == battmon2_get_battery_capacity ( &battmon2, &capacity ) )
{
log_printf ( &logger, " Capacity: %.1f mAh \r\n", capacity );
}
if ( BATTMON2_OK == battmon2_get_battery_percentage ( &battmon2, &percentage ) )
{
log_printf ( &logger, " Percentage: %.1f %% \r\n", percentage );
}
if ( current > 0 )
{
uint32_t time_to_full;
if ( BATTMON2_OK == battmon2_get_battery_ttf ( &battmon2, &time_to_full ) )
{
log_printf ( &logger, " Time to full: %uh %umin %usec \r\n", ( uint16_t ) ( time_to_full / 3600 ),
( uint16_t ) ( time_to_full % 3600 ) / 60,
( uint16_t ) ( time_to_full % 60 ) );
}
}
else if ( current < 0 )
{
uint32_t time_to_empty;
if ( BATTMON2_OK == battmon2_get_battery_tte ( &battmon2, &time_to_empty ) )
{
log_printf ( &logger, " Time to empty: %uh %umin %usec \r\n", ( uint16_t ) ( time_to_empty / 3600 ),
( uint16_t ) ( time_to_empty % 3600 ) / 60,
( uint16_t ) ( time_to_empty % 60 ) );
}
}
if ( BATTMON2_OK == battmon2_get_die_temperature ( &battmon2, &die_temp ) )
{
log_printf ( &logger, " Internal temperature: %.2f C \r\n\n", die_temp );
}
Delay_ms ( 1000 );
}
@ BATTMON2_OK
Definition battmon2.h:265
err_t battmon2_get_die_temperature(battmon2_t *ctx, float *temperature)
BATT-MON 2 get die temperature function.
err_t battmon2_get_battery_capacity(battmon2_t *ctx, float *capacity)
BATT-MON 2 get battery capacity function.
err_t battmon2_get_battery_ttf(battmon2_t *ctx, uint32_t *time_to_full)
BATT-MON 2 get battery ttf function.
err_t battmon2_get_battery_tte(battmon2_t *ctx, uint32_t *time_to_empty)
BATT-MON 2 get battery tte function.
void application_task(void)
Definition main.c:70

Application Output

This Click board can be interfaced and monitored in two ways:

  • Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
  • UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.

Additional Notes and Information

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.